class Equipment extends PIXI.Container {
    constructor(character, uimanager, inventory) {
        super();
        this.character = character;
        this.uimanager = uimanager;
        this.inventory = inventory;
        this.inventory.hideArrowButton();

        // 添加菜单
		this.menu = new Menu("EQUIP", 220, 170);
		this.addChild(this.menu);
		if(!this.menuTween) {
			this.menu.x = this.uimanager.realScreen.width;
			this.menu.y = 155;
			this.menuTween = createjs.Tween.get(this.menu.position).to({x:395,y:155},200);
		} else {
			this.menu.x = 395;
			this.menu.y = 155;
		}

		// 添加选单到Panel内
        this.updateMenu();
    }

    updateMenu() {
        var tmp = this.menu.selectedIndex;
        this.menu.clearItems();

        for(var i = 0; i < this.character.equipments.length; i++) {
            var equip = this.character.equipments[i];
            if(equip) {
                const menuItem = new MenuItem(equip.name);
                menuItem.icon = equip.getIcon();
                menuItem.tag = equip;
                this.menu.addItem(menuItem);
            }
            else {
                this.menu.addItem(new MenuItem("-"));
            }
        }

        if(tmp<0) tmp = 0;
        this.menu.selectedIndex = tmp;
    }

    reset() {
        this._inHiding = false;
        this.menu.selected.buttonVisual = true;
        this.menuTween = createjs.Tween.get(this.menu.position).to({x:395,y:155},200);
    }

    keyDown(key) {
        switch(key) {
            case "up": this.menu.up(); break;
			case "down": this.menu.down(); break;
            case "z":
                if(!this._inHiding) {
                    this._inHiding = true;
                    const uimanager = this.uimanager;
                    const equipment = this;

                    // 隐藏按钮
                    this.menu.selected.buttonVisual = false;
                    // 隐藏数值栏
                    this.inventory.hideData();

                    // 检查物品是否可装备
                    function checkCanUse(item) {
                        // location 3 可以装备2个
                        // 得到当前角色locations
                        const locations = [];
                        for(var i = 0; i < 5; i++)
                            locations.push(0);

                        for(var i = 0; i < equipment.character.equipments.length; i++) {
                            var equip = equipment.character.equipments[i];
                            if(equip && i != equipment.menu.selectedIndex) {
                                locations[equip.location]++;
                            }
                        }

                        if(locations[item.location] < 1 || (item.location == 3 && locations[3] < 2)) 
                            return true;

                        return false;
                    }

                    global.bag.setup(
                       checkCanUse,
                        function(item) {
                            if(!checkCanUse(item)) {
                                // TODO 播放停止声

                                return;
                            }

                            // 按下z装备道具
                            item.apply(equipment.character, equipment.menu.selectedIndex);
                            // 更新道具栏
                            equipment.updateMenu();
                            // 更新数值
                            equipment.inventory.updateData();
                            // 恢复状态
                            equipment.reset();
                            uimanager.removeChild(global.bag);
                            // TODO 播放成功声
                        },
                        function() {
                            equipment.updateMenu();
                            equipment.inventory.updateData();
                            equipment.reset();
                            uimanager.removeChild(global.bag);
                        });

                    this.menuTween = createjs.Tween.get(this.menu.position).to({x:this.uimanager.realScreen.width-130,y:155},200).call(function() {
                        global.bag.x = 60;
                        global.bag.y = 155;
                        uimanager.addChild(global.bag);
                    });
                }
                break;
            case "x":
                if(!this._inHiding) {
                    this._inHiding = true;
                    const uimanager = this.uimanager;
                    const inventory = this.inventory;
                    createjs.Tween.get(this.position).to({x:uimanager.realScreen.width,y:0},200).call(function() {
                        uimanager.removeChild(uimanager.children[uimanager.children.length - 1]);
                        inventory.resetMenu();
                        inventory.showArrowButton();
                    });
                }
                break;
        }
    }


}